﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PIM_ATE.Electrical;

namespace PIM_ATE
{
    public partial class FrmInstrumentsInfo : Form
    {
        ClsCalInstrumentsInfoBuilder m_InstrumentsInfoBuilder;
        ClsDataUser m_usrObj;
        public FrmInstrumentsInfo(ClsDataUser usrObj)
        {
            InitializeComponent();
            m_InstrumentsInfoBuilder = new ClsCalInstrumentsInfoBuilder(GDefine.DBNamePIM);
            m_usrObj = usrObj;
            cb_InsturmentType.SelectedIndex = 0;
            cb_InsturmentBand.SelectedIndex = 0;
        }

        private void FrmInstrumentsInfo_Load(object sender, EventArgs e)
        {
            getSource();
        }

        bool checkControl()
        {
            if (txt_InsturmentID.Text.Trim() == "" ||
               txt_InsturmentIP.Text.Trim() == "" ||
               txt_InstrumentPortCount.Text.Trim() == ""
               )
            {
                MessageBox.Show("不能为空！");
                return false;
            }
            else
                return true;
        }
        void getSource()
        {
            DataTable dt = m_InstrumentsInfoBuilder.getDtInstrumentInfo();
            dgvShow.DataSource = dt;
        }
        private void btn_add_Click(object sender, EventArgs e)
        {
            if (!checkControl()) return;
            ClsCalInstrumentsInfo info = new ClsCalInstrumentsInfo();
            info.PCIP = m_usrObj.sEquID;
            info.UserID = m_usrObj.sUser;
            info.InstrumentID = txt_InsturmentID.Text.Trim();
            info.InstrumentIP = txt_InsturmentIP.Text.Trim();
            info.InstrumentPortCount = txt_InstrumentPortCount.Text.Trim();
            info.InstrumentBand = cb_InsturmentBand.Text;
            info.InstrumentType = cb_InsturmentType.SelectedIndex.ToString();
            info.InstrumentCalSpec = nud_InstrumentCalSpec.Value.ToString();
            if (!m_InstrumentsInfoBuilder.insertData(info))
            {
                MessageBox.Show("上传失败！", "错误");
            }
            else
            {
                MessageBox.Show("上传成功！", "提示");
            }
            //刷新数据源
            getSource();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            if (dgvShow.SelectedRows != null)
            {
                if (dgvShow.CurrentCell == null || dgvShow.CurrentCell.RowIndex == -1)
                {
                    MessageBox.Show("请在列表中选择链路!", "错误");
                    return;
                }

                string sProjectId = dgvShow[0, dgvShow.CurrentCell.RowIndex].Value.ToString();
                if (sProjectId == "")
                {
                    MessageBox.Show("请在列表中选择链路!", "错误");
                    return;
                }

                if (MessageBox.Show("是否确认删除!", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
                string sSwitchId = dgvShow.CurrentRow.Cells[0].Value.ToString();
                if (!m_InstrumentsInfoBuilder.deleteData(sSwitchId))
                {
                    return;
                }
                dgvShow.Rows.RemoveAt(dgvShow.CurrentCell.RowIndex);
            }
            else
            {
                MessageBox.Show("请选中需要删除的行！", "错误");
            }
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            if (!checkControl()) return;
            if (dgvShow.SelectedRows != null)
            {
                if (dgvShow.CurrentCell == null || dgvShow.CurrentCell.RowIndex == -1)
                {
                    MessageBox.Show("请在列表中选择链路!", "错误");
                    return;
                }

                string sProjectId = dgvShow[0, dgvShow.CurrentCell.RowIndex].Value.ToString();
                if (sProjectId == "")
                {
                    MessageBox.Show("请在列表中选择链路!", "错误");
                    return;
                }

                if (MessageBox.Show("是否确认更新!", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
                //IDN唯一禁止更新，直接替换不提示
                txt_InsturmentID.Text = dgvShow.CurrentRow.Cells["InstrumentID"].Value.ToString();
                ClsCalInstrumentsInfo info = new ClsCalInstrumentsInfo();             
                info.ID = Convert.ToInt32(dgvShow.CurrentRow.Cells["ID"].Value.ToString());
                info.PCIP = m_usrObj.sEquID;
                info.UserID = m_usrObj.sUser;
                info.InstrumentID = txt_InsturmentID.Text.Trim();
                info.InstrumentIP = txt_InsturmentIP.Text.Trim();
                info.InstrumentPortCount = txt_InstrumentPortCount.Text.Trim();
                info.InstrumentBand = cb_InsturmentBand.Text;
                info.InstrumentType = cb_InsturmentType.SelectedIndex.ToString();
                info.InstrumentCalSpec = nud_InstrumentCalSpec.Value.ToString();
             
                if (!m_InstrumentsInfoBuilder.updateData(info))
                {
                    MessageBox.Show("更新失败!", "错误");
                    return;
                }
                else
                {
                    MessageBox.Show("更新成功!", "提示");
                }
                //刷新数据源
                getSource();
            }
            else
            {
                MessageBox.Show("请选中需要删除的行！", "错误");
            }
        }

        private void dgvShow_Click(object sender, EventArgs e)
        {
            try
            {
                txt_InsturmentID.Text = dgvShow.CurrentRow.Cells["InstrumentID"].Value.ToString();
                txt_InsturmentIP.Text = dgvShow.CurrentRow.Cells["InstrumentIP"].Value.ToString();
                cb_InsturmentBand.Text = dgvShow.CurrentRow.Cells["InstrumentBand"].Value.ToString();
                txt_InstrumentPortCount.Text = dgvShow.CurrentRow.Cells["InstrumentPortCount"].Value.ToString();
                cb_InsturmentType.SelectedIndex = Convert.ToInt32(dgvShow.CurrentRow.Cells["InstrumentType"].Value.ToString());
                nud_InstrumentCalSpec.Value = Convert.ToDecimal(dgvShow.CurrentRow.Cells["InstrumentCalSpec"].Value.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误");
            }  
        }

    }
}
